package com.neusoft.hifly.commons.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * Title: 核心类库
 * <p>
 * Description: 正则表达式
 * <p>
 * Copyright: 2019
 * <p>
 * Company: 东软汉枫
 *
 * @author lh_neu
 * @version 1.0.0
 */
public final class RegexpUtil {
	/**
	 * 特殊字符集
	 */
	public static final String[] SPECIAL_ARRAY = new String[] { "[", "]", "(", ")", "{", "}", "『", "』", "「", "」", "￥",
			"￡", "◎", "■", "●", "№", "↑", "→", "↓" + "!", "@", "#", "$", "%", "^", "&", "*", "_", "-", "+", "=", "|",
			"？", "~", "`" + "!", "<", ">", "‰", "→", "←", "↑", "↓", "¤", "§", "＃", "＆", "＆", "＼", "≡", "≠" + "≈", "∈",
			"∪", "∏", "∑", "∧", "∨", "⊥", "∥", "∥", "∠", "⊙", "≌", "≌", "√", "∝", "∞", "∮" + "∫", "≯", "≮", "＞", "≥",
			"≤", "≠", "±", "＋", "÷", "×", "／", "Ⅱ", "Ⅰ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅹ", "Ⅻ", "㈠", "㈡" + "╄", "╅",
			"╇", "┻", "┻", "┇", "┭", "┷", "┦", "┣", "┝", "┤", "┷", "┷", "┹", "╉", "╇", "【", "】" + "㈢", "㈣", "㈤", "㈥",
			"㈦", "㈧", "㈨", "㈩", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩", "┌", "├", "┬", "┼", "┍", "┕", "┗",
			"┏", "┅", "─" + "〖", "〗", "←", "〓", "☆", "§", "□", "‰", "◇", "︿", "＠", "△", "▲", "＃", "℃", "※", ".", "≈",
			"￠", "•", "π", "°", "€", "¢", "£", "¥", "™", "®", "©", "¶" };

	/**
	 * 合法的中文
	 */
	public static final String REGEXP_CHINESE = "^[\u4E00-\u9FA5]+$";

	/**
	 * 合法的数字
	 */
	public static final String REGEXP_NUMBER = "^\\d+$";

	/**
	 * 汉字,数字,字母,空格,下划线.不能以空格,下划线开头和结尾
	 */
	public static final String REGEXP_STRING = "^(?!_)(?!.*?_$)(?!\\s)(?!.*?\\s$)[a-zA-Z0-9\\s_\\u4e00-\\u9fa5]+$";

	/**
	 * 以英文字母开头，只能包含英文字母、数字、下划线
	 */
	public static final String REGEXP_NUMOREN = "^[a-zA-Z][a-zA-Z0-9_]*$";

	/**
	 * 只含有数字,字母,空格,下划线不能以空格,下划线开头和结尾
	 */
	public static final String REGEXP_NUMORENORUNDERLINE = "^(?!_)(?!.*?_$)(?!\\s)(?!.*?\\s$)[a-zA-Z0-9\\s_]+$";

	/**
	 * 是否英文
	 */
	public static final String REGEXP_ENGLISH = "^[A-Za-z]+$";

	/**
	 * yyyy-MM-dd格式的日期
	 */
	public static final String REGEXP_DATE = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

	/**
	 * yyyy-MM-dd HH:mm:ss格式的日期
	 */
	public static final String REGEXP_DATETIME = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";

	/**
	 * 手机号或集团号码
	 */
	public static final String REGEXP_MOBILE = "^(0?1([3,5,8]{1}[0-9]{1})[0-9]{8})|(\\d{6})$";

	/**
	 * 8888-8888888等格式的固定电话
	 */
	public static final String REGEXP_TEL = "^([0-9][1-9][0-9]{1,2}-)[0-9]{7,8}$";

	/**
	 * 合法的Email地址
	 */
	public static final String REGEXP_EMAIL = "^[0-9a-zA-Z_\\-\\.]+@[0-9a-zA-Z_\\-]+(\\.[0-9a-zA-Z_\\-]+)*$";

	/**
	 * 合法的URL地址
	 */
	public static final String REGEXP_URL = "^(file|http|https|ftp|mms|telnet|news|wais|mailto):\\/\\/(.+)$";

	/**
	 * 合法的域名
	 */
	public static final String REGEXP_DOMAIN = "^[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\\.?$";

	/**
	 * 合法的IP地址
	 */
	public static final String REGEXP_IP = "^(25[0-5]|2[0-4]\\d?|1\\d{2}|[1-9]\\d?)\\."
			+ "(25[0-5]|2[0-4]\\d?|1\\d{2}|[1-9]\\d?|0)\\." + "(25[0-5]|2[0-4]\\d?|1\\d{2}|[1-9]\\d?|0)\\."
			+ "(25[0-5]|2[0-4]\\d?|1\\d{2}|[1-9]\\d?)$";

	/**
	 * 15位或18位的身份证号
	 */
	public static final String REGEXP_IDENTITYCARD = "(^[0-9]{15}$)|(^[0-9]{17}([0-9]|X|x)$)";

	/**
	 * 6位数的邮编地址
	 */
	public static final String REGEXP_ZIPCODE = "^\\d{6}$";

	/**
	 * 校验QQ
	 */
	public static final String REGEXP_QQ = "^\\d{5,12}$";

	/**
	 * 正浮点数,最多2位小数,如果第一位为0则下一位必须为小数点
	 */
	public static final String REGEXP_FLOAT = "^0?(([1-9][0-9]*[.]?)|(0.)|(0))([0-9]{0,2})?$";

	/**
	 * 匹配移动手机号
	 * <p>
	 * 移动的号段：134(0-8)、135、136、137、138、139、147（预计用于TD上网卡）、150、151、152、157（TD专用）、158、159、187（未启用）、188（ TD专用）
	 */
	public static final String PATTERN_CMCMOBILENUM = "^1(3[4-9]|5[012789]|8[78])\\d{8}$";

	/**
	 * 匹配电信手机号
	 * <p>
	 * 电信的号段：133、153、180（未启用）、189
	 */
	public static final String PATTERN_CTCMOBILENUM = "^18[09]\\d{8}$";

	/**
	 * 匹配联通手机号
	 * <p>
	 * 联通的号段：130、131、132、155、156（世界风专用）、185（未启用）、186（3g）
	 */
	public static final String PATTERN_CUTMOBILENUM = "^1(3[0-2]|5[56]|8[56])\\d{8}$";

	/**
	 * 匹配CDMA手机号
	 * <p>
	 * CDMA的号段：133、153
	 */
	public static final String PATTERN_CDMAMOBILENUM = "^1[35]3\\d{8}$";

	/**
	 * 校验
	 *
	 * @param pattern
	 *            正则表达式
	 * @param value
	 *            被校验的值
	 * @return true正确/false错误
	 */
	public static boolean validate(final String pattern, final String value) {
		if (StringUtils.isEmpty(pattern)) {
			return false;
		}
		final Pattern pt = Pattern.compile(pattern);
		final Matcher matcher = pt.matcher(value);
		if (matcher.matches()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 测试
	 *
	 * @param args
	 *            参数
	 */
	public static void main(final String[] args) {
		System.out.println(RegexpUtil.validate(RegexpUtil.REGEXP_NUMOREN, "e3323"));
		System.out.println(RegexpUtil.validate(RegexpUtil.REGEXP_NUMOREN, "1ee223"));
		System.out.println(RegexpUtil.validate(RegexpUtil.REGEXP_NUMOREN, "啊12"));
		System.out.println(RegexpUtil.validate(RegexpUtil.REGEXP_NUMOREN, "12啊"));
		System.out.println(RegexpUtil.validate(RegexpUtil.REGEXP_NUMOREN, "r3212啊"));
	}
}
